package com.sharedlease.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sharedlease.entity.Share_order;
import com.sharedlease.entity.Share_product;
import com.sharedlease.entity.R;
import com.sharedlease.service.Share_productService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴钟富
 * @since 2021-03-22
 */
@RestController
@Api(tags = "共享产品（件）相关接口")
@RequestMapping("/share-product")
public class Share_productController {
    @Autowired
    public Share_productService share_productService;

    @GetMapping("/checkRented/{id}")
    @ApiOperation("根据一个id查询共享产品（件）是否已被租用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要查询的共享产品（件）id", defaultValue = "1"),

    }
    )
    public boolean checkRented(@PathVariable Long id) {
        Share_product share_product = share_productService.getById(id);
        if (share_product.getRented()==1){
            return true;
        }else{
            return false;

        }
    }

    @PutMapping("/updateRentedById/{id}/{rented}")
    @ApiOperation("根据id更改共享产品是否被租用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要查询的共享产品id", defaultValue = "1"),
            @ApiImplicitParam(name = "rented", value = "传过来的rented为目标状态。1为已租，0为未租", defaultValue = "1")

    }
    )
    R updateRentedById(@PathVariable Long id,@PathVariable Integer rented) {
        UpdateWrapper<Share_product> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        Share_product share_product = new Share_product();
        share_product.setRented(rented);
        if (share_productService.update(share_product,wrapper)) {
            return R.ok();
        }
        return R.error();
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("根据一个id查询共享产品（件）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要查询的共享产品（件）id", defaultValue = "1"),

    }
    )
    public Share_product getById(@PathVariable Long id) {
        Share_product share_product = share_productService.getById(id);
        return share_product;
    }

    @GetMapping("/getByShareId/{shareId}")
    @ApiOperation("根据一个shareId查询共享产品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shareId", value = "要查询的共享产品（件）shareId", defaultValue = "1"),

    }
    )
    public List<Share_product> getByShareId(@PathVariable Long shareId) {
        QueryWrapper<Share_product> wrapper = new QueryWrapper<>();
        wrapper.eq("shared_id",shareId);
        List<Share_product> list = share_productService.list(wrapper);
        return list;
    }

    @GetMapping("/list")
    @ApiOperation("查询全部共享产品（件）")
    public List<Share_product> list() {
        List<Share_product> list = share_productService.list(null);
        return list;
    }

    @PostMapping("/save")
    @ApiOperation("新增一条共享产品（件）")
    public R save(@RequestBody Share_product share_product) {

        return R.operate(share_productService.save(share_product));
    }

    @PutMapping("/update")
    @ApiOperation("根据id更改一条共享产品（件）")
    R update(@RequestBody Share_product share_product) {
        if (share_productService.updateById(share_product)) {
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据id删除一条共享产品（件）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要删除的共享产品（件）id", defaultValue = "-1"),

    }
    )
    R removeById(@PathVariable Long id) {

        return R.operate(share_productService.removeById(id));
    }

}

